/**
 *查询用户组下拉框
 */
'use strict';

const conn = require('../../libs/DB/Conn');
const _ = require("underscore");

module.exports = async function (params) {
   let result = await conn(`SELECT user_id AS value,user_name AS label,ug.group_name AS groupName,ug.group_id AS groupId FROM user_file uf INNER JOIN user_group ug ON ug.group_id = uf.group_id AND ug.delete_sign = 1 AND ug.is_manager = 0 WHERE uf.delete_sign = 1 AND uf.domain_id = ${params.userDomainId}`);
   let resultGroup = _.groupBy(result,"groupId");
   let downDropList = [];
   for(const groupId in resultGroup){
      let groupList =  resultGroup[groupId];
      let groupObj = {
         value:"g_"+groupList[0].groupId,
         label:groupList[0].groupName,
         children:[]
      };
      for(const user of groupList){
         groupObj.children.push({
            value:user.value,
            label:user.label,
         })
      }
      downDropList.push(groupObj);
   }

   return {success:true,downDropList:downDropList}
};